package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessExceptionHandler;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> selectList(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        List<User> list= userMapper.selectList(userDto);

        Page<User> p = (Page<User>) list;

        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String password = user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.insert(user);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    @Override
    public void update(User user) {
        user.setPassword(user.getUsername()+"123");
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public User selectById(Integer id) {
        User user=userMapper.selectById(id);
        return user;
    }

    @Override
    public List<User> selectByRole(String roleLabel) {
        List<User> list = userMapper.selectByRole(roleLabel);
        return list;
    }

    @Override
    public LoginResultVo login(User user) {
        User loginUser =userMapper.loginByUsername(user.getUsername());
        if (loginUser==null){
            log.info("用户{}不存在",user.getUsername());
            throw new BusinessExceptionHandler("用户或密码错误");
        }
        if (!loginUser.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
            log.info("用户{}密码错误",user.getUsername());
            throw new BusinessExceptionHandler("用户或密码错误");
        }
        if (loginUser.getStatus()!=1){
            log.info("用户{}被禁用",user.getUsername());
            throw new BusinessExceptionHandler("用户已停用");
        }
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(loginUser.getId());
        loginResultVo.setUsername(loginUser.getUsername());
        loginResultVo.setName(loginUser.getName());
        loginResultVo.setImage(loginUser.getImage());
        loginResultVo.setRoleLabel(loginUser.getRoleLabel());
        //令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",loginUser.getId());
        claims.put("username",loginUser.getUsername());
        String token = JwtUtils.generateToken(claims);
        loginResultVo.setToken(token);

        return loginResultVo;
    }
}
